import { defineStore } from 'pinia'
import TxMapHandler from '@/utils/TXMap/txMapHandler'


export const useUserStore = defineStore('user', {
  state: () => ({
    /**
     * 用户基本信息
     * | 参数 | 含义 |
     * | :------ | :------|
     * | nickname | 昵称 |
     * | openId | openId |
     * | phone | 电话 |
     * | unionId | unionId |
     * | userId | 用户唯一id |
     * ...
     */
    userInfo: uni.getStorageSync('userInfo'),
    userId: '',
    // 用户的定位信息
    userLocation: {},
  }),
  actions: {
    async getUserInfo () {
      console.log('👤userInfo', this.userInfo)
      return this.userInfo
    },
    setUserInfo (info: any) {
      this.userInfo = info
      logger.info('userInfo', info)
    },
    getUserLocation () {
      const txMapHandler = new TxMapHandler()
      txMapHandler.reverseGeocoder().then(async (res: any) => {
        const cityName = res.result.address_component.city
        // 获取城市id接口获取
        // const cityId = (await http.getCityId({ cityName })).data
        const cityId = ''
        const location = res.result.location // 经纬度信息
        logger.info('getUserLocation:', res)
        this.userLocation = {
          cityId,
          cityName,
          location,
        }
        return this.userLocation
      })
    },
  },
})
